package me.hl.stream;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created by Planck on 2016/3/24.
 */
public class RandomAccessFileTest {
    @Test
    public void randomRedTest() {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(new File("d://sinaPost.txt"), "rw");
            byte[] buff = new byte[1024];
            //指针指向
            randomAccessFile.seek(10);
            while (randomAccessFile.read(buff) > 0) {
                System.out.print(new String(buff, "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void randomWriTest() {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(new File("d://sinaPost.txt"), "rw");
            String str = "aaaaaa";
            randomAccessFile.seek(randomAccessFile.length());
            byte[] byteStr = str.getBytes("UTF-8");
            randomAccessFile.write(byteStr);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}
